<?
class ContactMethod
{
	public $id;
	public $UserId;
	public $TypeId;
	public $Value;
	
	static function Save($rs)
	{
		$conn = GetConnection();
		$rsClean = escapeAllFields($rs, $conn);
		
		if(isset( $rs['id']) && $rs['id'] != '' )
		{
			$sql = "Update ContactMethods SET "
				. "UserId = '$rsClean[UserId]', "
				. "TypeId = '$rsClean[TypeId]', "
				. "Value = '$rsClean[Value]' "
				. "WHERE id=$rsClean[id] ";
		}else{
			$sql = "Insert Into ContactMethods (UserId, TypeId, Value) Values ("
				. "'$rsClean[UserId]', '$rsClean[TypeId]', '$rsClean[Value]' )";
		}
		$conn->query( $sql );
		$error = $conn->error;
		$conn->close();
		if($error != '') throw new Exception("Error:$error\nSql:$sql");
	}

}
//***************************************************	
function InitContactMethod($rs)
{
	$retVal = new ContactMethod();
	
	$retVal->id = isset($rs['id']) ? $rs['id'] : null;
	$retVal->UserId = $rs['UserId'];
	$retVal->TypeId = $rs['TypeId'];
	$retVal->Value = $rs['Value'];
	
	return $retVal;
}
//***************************************************	
function GetContactMethod($id)
{
	$conn = GetConnection();
	$results = $conn->query("SELECT * FROM ContactMethods WHERE id=$id");
	if($rs = $results->fetch_assoc())
	{
		$conn->close();
		return $rs;
	}
	$conn->close();
	return null;
}
//***************************************************	
function GetContactMethods()
{
	$conn = GetConnection();
	$results = $conn->query("SELECT U.FirstName, U.LastName, CM.* FROM ContactMethods CM Join Users U ON CM.UserId=U.id");
	return $results;
}
//***************************************************	
function GetContactMethods2($id)
{
	$conn = GetConnection();
	$results = $conn->query("SELECT CM.* FROM ContactMethods CM WHERE UserId=$id");
	return $results;
}
//***************************************************	
function ValidateContactMethod($rs)
{
	$returnValue = null;

	if(!isset($rs['Value']) || $rs['Value'] == '')
		$returnValue['Value'] = 'Is Required';

	return $returnValue;
}
